package com.google.code.opengw.server.domain;

import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;

import com.google.code.opengw.policy.Policy;
import com.google.code.opengw.support.OpenGWException;

public class Route {

	private String publishName;
	private String routeName;
	private String requestPath;
	private String routeURL;
	private String routeHost;
	private String routePath;
	private int routePort;
	private boolean routeSecure;
	private String resourceURL;
	private String contentType;
	private String availableMethods;
	private boolean pathExtensionAvaliable;
	private boolean allowParameters;
	private int connectionTimeout;
	private int readTimeout;
	private Policy[] policyList;
	private RouteType routeType;

	public Route(String publishName, String routeName, String requestPath, String routeURL,
			String resourceURL, String contentType, String availableMethods, boolean pathExtensionAvaliable,
			boolean allowParameters, int connectionTimeout, int readTimeout, RouteType routeType,
			Policy[] policyList) throws IOException {
		this.publishName = publishName;
		this.routeName = routeName;
		this.requestPath = requestPath;		
		this.resourceURL = resourceURL;
		this.contentType = contentType;
		this.availableMethods = availableMethods;
		this.pathExtensionAvaliable = pathExtensionAvaliable;
		this.allowParameters = allowParameters;
		this.connectionTimeout = connectionTimeout;
		this.readTimeout = readTimeout;
		this.policyList = policyList;
		this.routeType = routeType;
		this.routeURL = routeURL;
		if(!routeURL.equalsIgnoreCase("none")){
			URL url = new URL(routeURL);
			routeHost = url.getHost();
			routePort = url.getPort();
			routePath = url.getPath();
			routeSecure = routeURL.startsWith("https://");
			if(routePort == -1 && routeSecure)
				routePort = 443;
			else if(routePort == -1)
				routePort = 80;
			
		}
	}
	
	public boolean isPolicyExists(){
		return policyList.length > 0 ? true : false;
	}
	
	public boolean controlRequestMethod(String requestMethod){
		return availableMethods.contains(requestMethod);
	}

	public String getPublishName() {
		return publishName;
	}

	public void setPublishName(String publishName) {
		this.publishName = publishName;
	}

	public String getRouteName() {
		return routeName;
	}

	public void setRouteName(String routeName) {
		this.routeName = routeName;
	}

	public String getRequestPath() {
		return requestPath;
	}

	public void setRequestPath(String requestPath) {
		this.requestPath = requestPath;
	}

	public String getRouteURL() {
		return routeURL;
	}

	public void setRouteURL(String routeURL) {
		this.routeURL = routeURL;
	}

	public String getResourceURL() {
		return resourceURL;
	}

	public void setResourceURL(String resourceURL) {
		this.resourceURL = resourceURL;
	}

	public String getContentType() {
		return contentType;
	}

	public void setContentType(String contentType) {
		this.contentType = contentType;
	}

	public String getAvailableMethods() {
		return availableMethods;
	}

	public void setAvailableMethods(String availableMethods) {
		this.availableMethods = availableMethods;
	}

	public boolean isAllowParameters() {
		return allowParameters;
	}

	public void setAllowParameters(boolean allowParameters) {
		this.allowParameters = allowParameters;
	}

	public boolean isPathExtensionAvaliable() {
		return pathExtensionAvaliable;
	}

	public void setPathExtensionAvaliable(boolean pathExtensionAvaliable) {
		this.pathExtensionAvaliable = pathExtensionAvaliable;
	}

	public int getConnectionTimeout() {
		return connectionTimeout;
	}

	public void setConnectionTimeout(int connectionTimeout) {
		this.connectionTimeout = connectionTimeout;
	}

	public int getReadTimeout() {
		return readTimeout;
	}

	public void setReadTimeout(int readTimeout) {
		this.readTimeout = readTimeout;
	}

	public Policy[] getPolicyList() {
		return policyList;
	}

	public void setPolicyList(Policy[] policyList) {
		this.policyList = policyList;
	}

	public RouteType getRouteType() {
		return routeType;
	}

	public void setRouteType(RouteType routeType) {
		this.routeType = routeType;
	}

	public String getRouteHost() {
		return routeHost;
	}

	public int getRoutePort() {
		return routePort;
	}

	public boolean isRouteSecure() {
		return routeSecure;
	}

	public String getRoutePath() {
		return routePath;
	}

	@Override
	public String toString() {
		return "Route [publishName=" + publishName + ", routeName=" + routeName
				+ ", requestPath=" + requestPath + ", routeURL=" + routeURL
				+ ", resourceURL=" + resourceURL + ", contentType="
				+ contentType + ", availableMethods=" + availableMethods
				+ ", allowParameters=" + allowParameters
				+ ", connectionTimeout=" + connectionTimeout + ", readTimeout="
				+ readTimeout + ", routeType=" + routeType + "]";
	}
	

}
